const cache = new Cache(config.timeout * 3)
const data = [0, 0, 0, 0, 0]

async function main(market, base, quote) {
	const url = `https://api.huobi.pro/market/depth?symbol=${base}${quote}&type=step0`
	try {
		const {statusCode, body} = await Request.getAsync(url, {timeout: config.timeout, json: true})
		// console.log(url, statusCode, body)

		if (statusCode == 200 && body.tick) {
			datas = body.tick
			data[1] = Math.min(...datas.asks.map(item => item[0]))
			data[2] = Math.max(...datas.bids.map(item => item[0]))
		    data[3] = Math.min(...datas.asks.map(item => item[1]))
			data[4] = Math.max(...datas.bids.map(item => item[1]))
			cache.put('data', data)
		}
	} catch (error) {
		LOG([market, error.code || error.message || error])
	}
	
	await Promise.delay(config.timeout * 0.5)
	main(market, base, quote)
}

exports.init = main
exports.data = cache